//
//  UserInfoDB.m
//  UI-豆瓣项目APP
//
//  Created by liuxin on 15/11/3.
//  Copyright © 2015年 孙鹏. All rights reserved.
//

#import "UserInfoDB.h"
#import <sqlite3.h>
@implementation UserInfoDB
+(instancetype)shareDataBase{
    static UserInfoDB *dataBase = nil;
    if(dataBase == nil){
        dataBase = [[UserInfoDB alloc]init];
    }
    return dataBase;
}
static sqlite3 *db = nil;
//sqlite数据库是基于C语言的底层数据库

//打开数据库
-(void)openDB{
    if(db != nil){
        NSLog(@"数据库已经打开了");
        return ;
    }
    //    打开数据库
    //    参数1:数据库文件
    //    参数2:数据库对象
    NSString *sqlFile = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *sqlPath = [sqlFile stringByAppendingPathComponent:@"userInfo.sqlite"];
    NSLog(@"%@",sqlPath);
    //    需要转成utf8的中文编码
    BOOL result =  sqlite3_open(sqlPath.UTF8String, &db);
    if(result == SQLITE_OK){
        NSLog(@"yes 打开成功");
    }else{
        NSLog(@"no 打开失败");
    }
}
//建表
-(void)createTable{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS userInfo(number INTEGER PRIMARY KEY AUTOINCREMENT,userName TEXT,password TEXT,surePassword TEXT,e_mail TEXT,phoneNumber TEXT)";
    BOOL result =  sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if(result == SQLITE_OK){
        NSLog(@"创建表成功");
    }
}

-(void)insertUser:(User *)user{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO userInfo(userName,password,surePassword,e_mail,phoneNumber)VALUES ('%@','%@','%@','%@','%@')",user.userName,user.password,user.surePassword,user.e_mail,user.phoneNumber];
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if(result == SQLITE_OK){
        NSLog(@"插入成功");
    }
}
-(NSMutableArray*)selectUser:(NSString*)string{
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM userInfo WHERE userName = '%@'",string];
    //    创建查询数据库跟随指针
    sqlite3_stmt *stmt = nil;
    //    查询前的准备工作
    //    参数3:限制sql语句长度,-1为不限制
    //    将数据库对象db,sql语句,跟随指针对象stmt关联一起
    BOOL result = sqlite3_prepare(db, sql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"准备查询成功");
    }else{
        NSLog(@"查询失败");
    }
    //    循环遍历每一行数据
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        //        取出第一列
        //        int number =  sqlite3_column_int(stmt, 0);
        //        取出第二列数据
        const unsigned char *username = sqlite3_column_text(stmt, 1);
        //        取出第三列数据
        const unsigned char *password = sqlite3_column_text(stmt, 2);
        //        取出第四列数据
        const unsigned char *surepassword = sqlite3_column_text(stmt, 3);
        const unsigned char *e_mail = sqlite3_column_text(stmt, 4);
        const unsigned char *phoneNumber = sqlite3_column_text(stmt, 3);

        //        将C语言类型转换为OC的
        //        NSInteger num = number;
        NSString *userName = [NSString stringWithUTF8String:(const char*)username];
        NSString* Password = [NSString stringWithUTF8String:(const char*)password];
        NSString *surePassword = [NSString stringWithUTF8String:(const char*)surepassword];
        NSString *E_mail = [NSString stringWithUTF8String:(const char*)e_mail];
        NSString *phonenumber = [NSString stringWithUTF8String:(const char*)phoneNumber];
        User *user = [User new];
        user.userName = userName;
        user.password = Password;
        user.surePassword = surePassword;
        user.e_mail = E_mail;
        user.phoneNumber = phonenumber;
        [array addObject:user];
    }
    return array;
}
@end
